
var APP=null;
(function() {
	var Designer = function(){};

	var FILE_BAR = [
		{name: 'new', img: '../images/file_new.png', title: 'New'},
		{name: 'open', img: '../images/file_open.png', title: 'Open'},
		{name: 'save', img: '../images/file_save.png', title: 'Save'},
		{name: null, img: null, title: null}
	];

	var EDIT_BAR = [
		{name: 'cut', img: '../images/edit_cut.png', title: 'Cut'},
		{name: 'copy', img: '../images/edit_copy.png', title: 'Copy'},
		{name: 'paste', img: '../images/edit_paste.png', title: 'Paste'},
		{name: null, img: null, title: null}
	];

	var VIEW_BAR = [
		{name: 'zoomin', img: '../images/zoomin.png', title: 'Zoom In (i)'},
		{name: 'zoomout', img: '../images/zoomout.png', title: 'Zoom Out (o)'},
		{name: null, img: null, title: null}
	];

	var DESIGN_BAR = [
		{name: 'component', img: '../images/component.png', title: 'Place component(pc)'},
		{name: 'track', img: '../images/track.png', title: 'Place track(pt)'},
		{name: 'wire', img: '../images/wire.png', title: 'Place wire(pw)'},
		{name: null, img: null, title: null}
	];

	var HELP_BAR = [
		{name: 'help', img: '../images/help.png', title: 'Help'},
		{name: null, img: null, title: null},
	];


	Designer.prototype.init_toolbar = function() {
		this.toolBar = new G.Widget.ToolBar(
			{	'file': FILE_BAR, 
				'edit': EDIT_BAR, 
				'view': VIEW_BAR, 
				'design': DESIGN_BAR,
				'help': HELP_BAR
			});
		this.toolBar.render('TOOL_BAR');

		this.toolBar.bind('view/zoomin', this.board.zoom_in, this.board);
		this.toolBar.bind('view/zoomout', this.board.zoom_out, this.board);

		this.toolBar.bind('design/component', this.bbDesigner.mouse_draw_component, this.bbDesigner);
		this.toolBar.bind('design/track', this.bbDesigner.mouse_draw_track, this.bbDesigner);
		this.toolBar.bind('design/wire', this.bbDesigner.mouse_draw_wire, this.bbDesigner);

		this.toolBar.bind('file/new', this.file_new, this);
		this.toolBar.bind('file/save', this.file_save, this);
		this.toolBar.bind('help/help', this.help, this);
	}

	Designer.prototype.help = function()
	{
		alert(" Hot Keys : \n"
				+ "'i':			zoom in,\n"
				+ "'o':			zoom out,\n"
				+ "'l':			swith top/bottom view,\n"
				+ "'d':			delete selected,\n"
				+ "'space':		rotate component/track in drawing,\n"
				+ "'pc':		place component,\n"
				+ "'pt':		place track,\n"
				+ "'pw':		place wire,\n"
				+ "'q':			return to select&drag mode\n"
			);
	}

	Designer.prototype.file_new = function()
	{
		this.libDesigner.run();
	}

	Designer.prototype.file_save = function()
	{
		this.libDesigner.save_symbol();
	}

	Designer.prototype.zoom_in = function()
	{
		if (this.board)	this.board.zoom_in();
	}

	Designer.prototype.zoom_out= function()
	{
		if (this.board)	this.board.zoom_out();
	}


	Designer.prototype.create_sb_field= function(w, locate) 
	{
		var locate = locate || 'l';
		console.log(locate);
		var div = document.createElement('div');
		div.className = 'statusbar';
		div.style.min_width = w;

		// Float it to right
		if (locate == 'r') {
			div.className += ' right';
		}
		console.log(div.style);
		this.statusBar.appendChild(div);
		return div;
	}

	Designer.prototype.init_statusbar = function() 
	{
		this.statusBar = document.getElementById('bottom');
		this.sbCoordinate = this.create_sb_field(100, 'r');
		this.sbScale = this.create_sb_field(100, 'r');
		this.sbDrawMode = this.create_sb_field(200);
	}

	Designer.prototype.init_project_view = function()
	{
		this.prjView = G('PRJ_VIEW').BarView([
				{name: 'board', title: 'Bread Board'},
				{name: 'library', title: 'Component Lib'}
		]);

		var em = this.prjView.get_body('board');
		var bdview = G(em).ListMenu([
			{'name': 'top', 'title': 'Top View' },
			{'name': 'bottom', 'title': 'Bottom View' }
		]);

		bdview.bind('onitemclick', this.view_layer, this);
		bdview.select_item(0);
		this.view_layer('top');


		var em = this.prjView.get_body('library');
		this._libTreeView = G(em).TreeView();

		this._libTreeView.link_data(this.bbLib.get_tree());
		this._libTreeView.bind('onselectchanged', this.on_lib_click, this);
	}

	Designer.prototype.view_layer= function(layer)
	{
		if (this.currentEditor != 'BBDesigner') {
			this.bbDesigner.run();
			this.currentEditor = 'BBDesigner';
		}
		this.bbDesigner.view_layer(layer);
	}


	Designer.prototype.on_lib_click = function(evt)
	{
		var sym = evt.node.data;

		if (typeof(sym.data) == 'object') {
			this.bbDesigner.set_current_symbol(sym);
		}
	}


	Designer.prototype.init = function() 
	{
		this.board = G('center').SVGBoard(4000, 3000);

		this.bbDesigner = new G.APP.BBoardDesigner(this.board);
		this.libDesigner = new G.APP.BBLibDesigner(this.board);
		this.bbLib = new G.APP.BBLib(LIB_COMMON);

		this.init_toolbar();
		this.init_statusbar();
		this.init_project_view();

		this.emStatus = document.getElementById('bottom');
		this.board.bind('oncursormove', this.show_cursor_xy, this);
		this.board.bind('onscalechanged', this.show_scale, this);
		this.board.bind('ondrawmodechanged', this.show_draw_mode, this);

		this.currentEditor = null;

		this.new_project();
	}

	Designer.prototype.show_draw_mode = function(mode) 
	{
		this.sbDrawMode.innerHTML = mode;
	}
	Designer.prototype.show_scale = function(scale) 
	{
		this.sbScale.innerHTML = "X" + scale.scale;
	}

	Designer.prototype.show_cursor_xy = function(p) 
	{
		this.sbCoordinate.innerHTML = p.x + ', ' + p.y;
	}

	Designer.prototype.new_project = function()
	{
		//this.project = new G.APP.EDAProject();
		//this._schTreeView.link_data(this.project.schTree);
		//this._schTreeView.set_current('schematic/page1');
		//this.load_sch_page(this._schTreeView.get_current_data());
		//this.schEditor.run();
	}
	APP = new Designer();
})();

